package sz.com.postoffice.jxyzwgcase.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sz.com.postoffice.jxyzwgcase.service.UpdateDailyIncomeService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@Component
public class UpdateDailyIncome {

    private final UpdateDailyIncomeService updateDailyIncomeService;

    public UpdateDailyIncome(UpdateDailyIncomeService updateDailyIncomeService) {
        this.updateDailyIncomeService = updateDailyIncomeService;
    }

    @Scheduled(cron = "0 0 5 * * ?",zone = "Asia/Shanghai")//每天05:00:00
    public void collectDataFromADO() {
        Date date = new Date();//获取当天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        //减少一天
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        //格式化
        String reStr = sdf.format(dt1);
        updateDailyIncomeService.updateDailyIncomeByDate(reStr);
    }
}
